package com.og.ouroa.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.og.ouroa.common.annotations.EnableLoginHandle;
import com.og.ouroa.common.result.R;
import com.og.ouroa.pojo.entity.StudentNode;
import com.og.ouroa.pojo.entity.UserInfo;
import com.og.ouroa.pojo.vo.NodeVO;
import com.og.ouroa.service.StudentNodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.soap.Node;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Us
 * @since 2021-12-13
 */
@CrossOrigin
@RestController
@RequestMapping("/admin/studentNode")
@Api(tags = "笔记系统接口")
public class StudentNodeController {


    @Autowired
    StudentNodeService service;

    @EnableLoginHandle
    @ApiOperation("添加笔记")
    @PostMapping("/add")
    public R add(@RequestBody
                 @ApiParam(value = "笔记对象", required = true) NodeVO node) {

        service.addNode(node.getSid(), node.getTitle(), node.getArticle());

        return R.ok().message("插入成功");
    }

    @EnableLoginHandle
    @ApiOperation("删除笔记")
    @DeleteMapping("/remove/{id}")
    public R remove(@ApiParam(value = "数据id")
                    @PathVariable Integer id) {
        service.remove(id);

        return R.ok().message("删除成功");

    }

    @EnableLoginHandle
    @ApiOperation("精确查询")
    @GetMapping("/search/{sid}/{title}")
    public R search(
            @ApiParam(value = "用户id" ) @PathVariable Integer sid,
            @ApiParam(value = "文章标题" ) @PathVariable String title) {

        StudentNode search = service.search(sid, title);


        return R.ok().message("查询成功").data("查询结果",search);



    }

    @EnableLoginHandle
    @ApiOperation("模糊查询")
    @GetMapping("/fuzzySearch/{page}/{limit}/{title}")
    public R fuzzySearch(
            @ApiParam(value = "当前页码", required = true) @PathVariable Long page,
            @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit,
            @ApiParam(value = "模糊查询字段", required = true)@PathVariable String title
    ) {


        Page<StudentNode> pages = new Page<>(page, limit);
        IPage<StudentNode> search = service.fuzzySearch(pages, title);
        System.out.println(title + " =========="+search.getRecords());
        return R.ok().message("查询成功").data("当前页",search);

    }



    @EnableLoginHandle
    @ApiOperation("时间倒序查询")
    @GetMapping("/searchTop/{page}/{sid}/{limit}")
    public R searchTop(
            @ApiParam(value = "当前页码", required = true) @PathVariable Long page,
            @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit,
            @ApiParam(value = "用户id" )@PathVariable Integer sid
    ) {

        IPage<StudentNode> studentNodeIPage;
        Page<StudentNode> pages = new Page<>(page, limit);
        if(sid==null){
             studentNodeIPage = service.listTop(pages);
        }else {
             studentNodeIPage = service.listTopByid(pages, sid);

        }
        return R.ok().message("查询成功").data("当前页",studentNodeIPage);
    }

    @EnableLoginHandle
    @ApiOperation("修改笔记")
    @PostMapping("/update")
    public R update(@RequestBody
                 @ApiParam(value = "笔记对象", required = true) NodeVO node) {

        service.updata(node.getSid(), node.getTitle(), node.getArticle());

        return R.ok().message("修改成功");
    }

}

